package br.com.cubedesk.telas;

import br.com.cubedesk.interfaces.InterfaceEquipamento;
import br.com.cubedesk.repositorio.RepositorioEquipamento;

public class AdicionarEquipamento extends javax.swing.JFrame {

    public AdicionarEquipamento(int id) {
        initComponents();
        if (id == 0) {
            this.id.setText("...");
        } else {
            this.id.setText(String.valueOf(id));
            InterfaceEquipamento repo = new RepositorioEquipamento();
            String marca_ref = repo.pegarEquipamentosPorId(id).get(0).getMarcaEquipamento();
            this.marca.setSelectedItem(marca_ref);
            String modelo_ref = repo.pegarEquipamentosPorId(id).get(0).getModeloEquipamento();
            this.modelo.setText(modelo_ref);
            int status_ref = repo.pegarEquipamentosPorId(id).get(0).getStatusEquipamento();
            if (status_ref == 1) {
                this.ativo.setSelected(true);
            } else {
                this.ativo.setSelected(false);
            }
        }
        
        if (!this.id.getText().equals("...")) {
            this.salvar.setText("Alterar");
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        marca = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        modelo = new javax.swing.JTextField();
        ativo = new javax.swing.JCheckBox();
        salvar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        id = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Adicionar Equipamento");

        jLabel1.setText("Marca");

        marca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "BROTHER", "CANON", "COPYSTAR", "GESTETNER", "KONICA MINOLTA", "KYOCERA", "OKI", "RICOH", "SAMSUNG", "SHARP", "TOSHIBA", "OUTROS" }));

        jLabel2.setText("Modelo");

        ativo.setSelected(true);
        ativo.setText("Equipamento ativo?");

        salvar.setText("Salvar");
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });

        jLabel3.setText("ID");

        id.setText("...");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ativo)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(id)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(salvar))
                            .addComponent(marca, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(modelo, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(marca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(modelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ativo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(salvar)
                    .addComponent(jLabel3)
                    .addComponent(id))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        if (this.id.getText().equals("...")) {
            String marca_ref = this.marca.getSelectedItem().toString();
            String modelo_ref = this.modelo.getText();
            int ativo_ref;
            if(this.ativo.isSelected()){
                ativo_ref = 1;
            } else {
                ativo_ref = 2;
            }
            InterfaceEquipamento repo = new RepositorioEquipamento();
            repo.salvarEquipamento(marca_ref, modelo_ref, ativo_ref);
            this.dispose();
        } else {
            String marca_ref = this.marca.getSelectedItem().toString();
            String modelo_ref = this.modelo.getText();
            int ativo_ref;
            if(this.ativo.isSelected()){
                ativo_ref = 1;
            } else {
                ativo_ref = 2;
            }
            InterfaceEquipamento repo = new RepositorioEquipamento();
            repo.alterarEquipamento(ativo_ref, marca_ref, modelo_ref, ativo_ref);
            this.dispose();
        }
    }//GEN-LAST:event_salvarActionPerformed

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdicionarEquipamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AdicionarEquipamento(0).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox ativo;
    private javax.swing.JLabel id;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox marca;
    private javax.swing.JTextField modelo;
    private javax.swing.JButton salvar;
    // End of variables declaration//GEN-END:variables
}
